<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="monitorInfo"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => {this.columns = columns}"
      :isSelectColumn="false"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <Drawer
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="700"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <add :type="handleType" :rowData="formData" @close="closeHandler" @reAdd="reAdd"/>
    </Drawer>
    <Drawer
      :visible.sync="visibleEdit"
      :title="dialogTitleEdit"
      destroyOnClose
      width="700"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <edit :type="handleTypeEdit" :rowData="formDataEdit" @close="closeHandlerEdit" @reEdit="reEdit" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryBusinessCustomer } from '@/api/part/partInfoManage/contactsCustomer';
import add from './tabs/add';
import edit from './tabs/edit';

export default {
  name: 'monitorInfo',
  mixins: [dictionary],
  components: {
    add,
    edit
  },
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: queryBusinessCustomer,
      fetchParams: {},
      handleType: '',
      handleTypeEdit: '',
      formData: {},
      formDataEdit: {},
      dialogTitle: '',
      dialogTitleEdit: '',
      visible: false,
      visibleEdit: false
    };
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '业务往来客户类型',
          fieldName: 'customerTypeCode',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '客户代码',
          fieldName: 'customerCode',
          placeholder: '客户代码'
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '客户名称'
        }
      ];
    },
    createTableColumns: function() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: (props,h) => {
            return <span> {props.row.index + 1} </span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props,h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '客户代码',
          dataIndex: 'customerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户简称',
          dataIndex: 'customerShortName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户拼音',
          dataIndex: 'customerSpell',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户类型名称',
          dataIndex: 'customerTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮编',
          dataIndex: 'zipCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '传真',
          dataIndex: 'fax',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '税号',
          dataIndex: 'taxNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'contactorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人电话',
          dataIndex: 'contactorPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人手机',
          dataIndex: 'contactorMobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '结算对象代码',
          dataIndex: 'balObjCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '结算对象名称',
          dataIndex: 'balObjName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '加价率',
          dataIndex: 'priceAddRate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '签约开始日期',
          dataIndex: 'agreementBeginDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '签约结束日期',
          dataIndex: 'agreementEndDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合约编号',
          dataIndex: 'contractNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '价格系数',
          dataIndex: 'priceRate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售基价',
          dataIndex: 'salesBasePriceType',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '信用（元）',
          dataIndex: 'creditAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账期（天）',
          dataIndex: 'accountTerm',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '累计欠款（天）',
          dataIndex: 'totalArrearageTerm',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户收款类别',
          dataIndex: 'cusReceiveSort',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'oemTag',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '账龄',
          dataIndex: 'accountAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订货周期',
          dataIndex: 'lendTime',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '预收款',
          dataIndex: 'prePay',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '累计欠款（元）',
          dataIndex: 'totalArrearageAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '维修欠款',
          dataIndex: 'arrearageAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleTypeEdit = 'edit';
      this.formDataEdit = row;
      this.showHandler1();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `业务往来客户${this.conf[this.handleType]}`;
      this.visible = true;
    },
    showHandler1() {
      this.dialogTitleEdit = `业务往来客户${this.conf[this.handleTypeEdit]}`;
      this.visibleEdit = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    closeHandlerEdit(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    reAdd(a, b){
      this.visible = false;
      this.reSearch(a, b);
    },
    reEdit(a, b){
      this.visibleEdit = false;
      this.reSearch(a, b);
    },
    async reSearch(a, b){
      console.log(a, b);
      let res = {};
      b = a == 1 ? b : {};
      res = await queryBusinessCustomer(b);
      console.log(res);
      this.BaseTable.EXECUTE_INSERT(res.data.rows);
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style scoped>
</style>
